﻿/*
 * debugLog.h
 *
 *  Created on: Nov 7, 2011
 *      Author: foscam
 */

#ifndef DEBUGLOG_H_
#define DEBUGLOG_H_
#include <errno.h>
#include <string.h>
#include <string>
//#include <pthread.h>
#ifdef _WIN32
#include <time.h>
#else
#include <sys/time.h>
#endif
enum DebugLevel
{
    DEBUG_LEVEL_NONE,
    DEBUG_LEVEL_ERROR,
    DEBUG_LEVEL_WARNING,
    DEBUG_LEVEL_INFO,
    DEBUG_LEVEL_ALL,
};

// enum MODULE
// {
//     API,
//     COMMON_LIB,
//     WATCH_DOG,
//     CODEC,
//     WEB_SERVICE,
//     DEV_MNG,
//     STORAGE,
//     FW_UPGRADE,
//     IPC_AGENT,
//     GUI,
//     UIIMPL,
//     NETIF,
//     LRCF,
//     MEDIA_SERVER,
//     UNKNOWN = 18866,
// };

#define DEBUG_LOG_FIFO_NAME	        "/tmp/DEBUGLOG_FIFO.Log"
#define DEBUG_LOG_FIFO_SIZE			64<<10
extern int debugLogLevel;
extern void initDebug(void);
extern int StartLogProcess(void);//only can be called by one process [now webService]
extern int StartSendLog(const char *kpcIpPort);
extern int StopSendLog(void);

void printDebugLog(int module, int debugLevel, const char *file, const int line, const char *format, ...);
void printAPIErrorInfo(int module, const char *apiName, const char *file, const int line);

void printDebugLogToFIFO(int module, int debugLevel, const char *file, const int line, const char *format, ...);

//#if defined(DEBUG_LOG_PFFIFO)
#define DEBUG_LOG_TO_HDD     1
#define DEBUG_LOG_PRINTF     2
#define DEBUG_LOG_NONE       0

//#define DEBUG_LOG_MODE  DEBUG_LOG_TO_HDD   // 1 ?洢?????, 2, ???????????
#ifdef _WIN32
#define DEBUG_LOG_MODE  0   // 1 ?洢?????, 2, ???????????
#else
#define DEBUG_LOG_MODE  DEBUG_LOG_PRINTF   // 1 ?洢?????, 2, ???????????
#endif
#if  (DEBUG_LOG_MODE== DEBUG_LOG_TO_HDD)

#define DEBUG_ERROR(module, format...) printDebugLogToFIFO(module, DEBUG_LEVEL_ERROR, __FILE__, __LINE__, format)

#define DEBUG_WARNING(module, format...) printDebugLogToFIFO(module, DEBUG_LEVEL_WARNING, __FILE__, __LINE__, format)

#define DEBUG_INFO(module, format...) printDebugLogToFIFO(module, DEBUG_LEVEL_INFO, __FILE__, __LINE__, format)

#define DEBUG_API_ERROR(module, apiName) printAPIErrorInfo(module, apiName, __FILE__, __LINE__)

#elif (DEBUG_LOG_MODE==DEBUG_LOG_PRINTF)

#define DEBUG_ERROR(module, format...) printDebugLog(module, DEBUG_LEVEL_ERROR, __FILE__, __LINE__, format)

#define DEBUG_WARNING(module, format...) printDebugLog(module, DEBUG_LEVEL_WARNING, __FILE__, __LINE__, format)

#define DEBUG_INFO(module, format...) printDebugLog(module, DEBUG_LEVEL_INFO, __FILE__, __LINE__, format)

#define DEBUG_API_ERROR(module, apiName) printAPIErrorInfo(module, apiName, __FILE__, __LINE__)

#else

#define DEBUG_ERROR(module, format)

#define DEBUG_WARNING(module, format)

#define DEBUG_INFO(module, format)

#define DEBUG_API_ERROR(module, apiName)

#endif

#define AUTO_LOG_FUN_LIFE(module) AutoLogFunLife AutoLogFunLife_V(module,__FUNCTION__, __FILE__, __LINE__)
#define AUTO_LOG_FUN_LIFE_N(module,num) AutoLogFunLife AutoLogFunLife_V_##num(module,__FUNCTION__, __FILE__, __LINE__)

#endif /* DEBUGLOG_H_ */


